import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

import matplotlib.pyplot as pyplot

# Load data from files (ensure to replace with actual file paths)
data11 = np.loadtxt('fig5_CDW_MI.dat')
data12 = np.loadtxt('fig5_DS.dat')
data13 = np.loadtxt('fig5_SS.dat')
data14 = np.loadtxt('fig5_DS-BG.dat')
data15 = np.loadtxt('fig5_inset.dat')

# Create the plot
fig, ax = plt.subplots(figsize=(10, 6))


# Adjust subplot layout using plt.subplots_adjust()
plt.subplots_adjust(left=0.11, bottom=0.14, right=1.0, top=0.97, 
                  wspace=0.2, hspace=0.0)  # Here you can change the values

# Set x and y axis range
ax.set_xlim(0, 0.09)
ax.set_ylim(0, 1.8)

# Set axis labels
ax.set_xlabel(r'$J/U$', fontsize=26, weight='bold')
ax.set_ylabel(r'$\mu/U$', fontsize=26, weight='bold')

# Customize ticks
ax.set_xticks([0,0.04,0.08])
ax.set_xticklabels([r'0.0', r'0.04', r'0.08'], fontsize=26)
#ax.tick_params(axis='y', labelsize=16)

ax.set_yticks([0, 0.5, 1, 1.5])
ax.tick_params(axis='y', labelsize=26)

# Plot the lines
ax.plot(data11[:, 0], data11[:, 1], label='',color='magenta',lw=3)
ax.plot(data12[:, 0], data12[:, 1], label='',color='green', lw=3)
ax.plot(data13[:, 0], data13[:, 1], label='',color='blue', lw=3)
ax.annotate('', xy=(0.028, 0.64), xytext=(0.006, 0.64),
            arrowprops=dict(facecolor='red', edgecolor='black', arrowstyle='->', lw=3))
ax.annotate('', xy=(0.028, 0.97), xytext=(0.004, 0.97),
            arrowprops=dict(facecolor='red', edgecolor='black', arrowstyle='->', lw=3))

ax.annotate('', xy=(0.047, 0.13), xytext=(0.006, 0.13),
            arrowprops=dict(facecolor='red', edgecolor='black', arrowstyle='->', lw=3))


# Plot points from fort87 file
sc = ax.scatter(data14[:, 1], data14[:, 0], c=data14[:, 2], cmap='viridis', s=20)  # Scatter plot with color mapping

# Adding text labels
ax.text(0.014, 0.41, 'CDW', ha='center', va='center', fontsize=26)
ax.text(0.05, 0.1, 'DS', ha='center', va='center', fontsize=26)
ax.text(0.0690634, 0.368913, 'SS', ha='center', va='center', fontsize=26)
ax.text(0.075, 1.1, 'SF', ha='center', va='center', fontsize=26)
ax.text(0.032, 0.65, 'DS', ha='center', va='center', fontsize=26)
ax.text(0.0314181, 0.968578, 'BG', ha='center', va='center', fontsize=26)
#ax.text(0.077, 1.78, r'$\frac{V_{1}}{U}=0.2$', ha='center', va='center', fontsize=16)
#ax.text(0.075, 1.67, r'$\frac{V_{2}}{U}=0$', ha='center', va='center', fontsize=16)
#ax.text(0.076, 1.56, r'$\frac{D}{U}=0.2$', ha='center', va='center', fontsize=16)
ax.text(0.014, 1.25, 'MI', ha='center', va='center', fontsize=26)
ax.text(0.084, 0.53, 'SF', ha='center', va='center', fontsize=26)

# Add a colorbar for the scatter plot
cbar = plt.colorbar(sc)
cbar.set_label('Color scale', fontsize=14)
cbar.set_label('')  # Remove the label next to the color bar

# Set the colorbar range from 0 to 1
cbar.ax.set_ylim(0, 1)

# Manually set the position and relative size of the inset axes within ax1

ax2 = plt.axes([0.515,0.6,0.29,0.35]) #(x_st, y_st, x_len, y_len)

ax2.set_xlim(0.56,1.05)
ax2.set_ylim(-0.005,1.005)
plt.yticks([0,0.5,1.0 ],['0','','1'], fontsize = 22)
plt.xticks([0.6,0.8,1.0],['0.6','0.8','1.0'], fontsize = 22)
ax2.plot(data15[:,0],data15[:,1],'green',linewidth = 3, marker = '')

ax2.set_xlabel(r'$\mu/U$', fontsize=22, weight='bold', labelpad=-0.05)
ax2.set_ylabel(r'$|S(\pi,\pi)|$', fontsize=22, weight='bold', labelpad=-0.05)

plt.plot([0.6,0.6],[0,1],'--',color = 'gray', linewidth=3)
plt.plot([0.91,0.91],[0,1],'--',color = 'gray', linewidth=3)
plt.plot([1,1],[0,1],'--',color = 'gray', linewidth=3)
plt.text(0.56, 0.5, r'CDW', color='black', rotation = 90, fontsize = 20)
plt.text(0.79, 0.52, r'DS', color='black', rotation = 90, fontsize = 20)
plt.text(0.93, 0.52,r'BG', color='black', rotation = 90, fontsize = 20)
plt.text(1.008, 0.52, r'MI', color='black', rotation = 90, fontsize = 20)



# Modify colorbar ticks
cbar.set_ticks([0, 0.5, 1])
cbar.ax.tick_params(labelsize=26)
fig.savefig('phase-ds-bg.pdf')
# Show the plot
plt.show()

